package main

import (
	"encoding/json"
	"fmt"
	"go-demos/mgr"
	"go-demos/test"
	"go-demos/utility"
	"log"
	"runtime"
	"time"
)
 
func main() {
	json_str2 :=mgr.GetConfigs("./config/config.json")
	p:=test.Person{}
	err:=json.Unmarshal([]byte(json_str2),&p)
	if(err!=nil){
		log.Fatal(err)
	}
	fmt.Printf("%#v\n",p)
	json_str := mgr.GetConfigs("./mgr/config.json")
	var workers []mgr.Worker
	workers = mgr.DeserializeJson(json_str)
	fmt.Printf("%#v\n",workers[0])
	fmt.Printf("%#v\n",workers[1])
	wait_chan := make(chan bool)
	go func(){
		c :=time.Tick(15*time.Second)
		for t := range c{
			fmt.Printf("the time is now :%v\n",t)
			for _,v := range workers{
				var Name string
				if(runtime.GOOS=="windows"){
					Name = v.Name+v.Extension
				}else{
					Name = v.Name
				}
				utility.CheckAndRun(v.Path,Name)
			}
		}
        // for _,v := range workers{
        //     utility.CheckAndRun(v.Path,v.Name)
        // }	
	}()
	go test.TestHttp()
    go func(){
        for v:= range test.DestroyChan{
            log.Println("destroy name:",v)
            utility.KillProcess(v)
        }
    }()
	if utility.IsProcessExists("chrome.exe"){
		log.Println("exists chrome.exe")
	}
	<-wait_chan
}